
axios.defaults.baseURL = 'http://localhost:8888';

Vue.prototype.$http = axios;

//======================axios的前置拦截器====================//
//每次路由请求之前：将token设置到请求头 -  token携带到后端
axios.interceptors.request.use(res=>{
    let token = localStorage.getItem("token");
    if(token){
        res.headers["token"] = token;
    }
    return res;
},error => {
    Promise.reject(error)
});

// 获取来自后端的相应，判断此时响应是否有 信息表示没有登陆
axios.interceptors.response.use(res => {
    let data = res.data;
    if (data.msg === "noLogin") {
       location.href = "login.html"
    }
    return res;
}, error => {
    Promise.reject(error);
});
  url = location.href;
  url = url.substring(url.indexOf("//") + 2);
  var arr = url.split("/");
  var tier = '';
  var i = 0;
  for(i; i < arr.length - 1; i++) {
      tier =  tier + "../"
  }

if (url.indexOf("login.html") === -1 &&
    url.indexOf("register.html") === -1 &&
    url.indexOf("callback.html") === -1 &&
    url.indexOf("binder.html") === -1 &&
    url.indexOf("index.html") === -1 ) {
    let loginInfo = localStorage.getItem("loginInfo");
    if (!loginInfo) {
        location.href = tier + "login.html"
    }
}

// 截取请求中的参数，封装成json对象
function parseUrlToJson(url) {
    var jsonObj = {};
    var urlItem = url.substring(url.indexOf("?") + 1);
    var arr = urlItem.split("&");
    for (var i = 0; i < arr.length; i++) {
        let data = arr[i];
        jsonObj[data.split("=")[0]] = data.split("=")[1];
    }
    return jsonObj;
}